<?php
/**
 * This file contains all of the default achievements.
 */

//WPMU
function dpa_register_blogcommentranked() { add_action( 'comment_post', 'dpa_handle_blogcommentranked', 10, 0 ); }
function dpa_register_blogranked() { add_action( 'publish_post', 'dpa_handle_blogranked', 10, 0 ); }
function dpa_register_blogwelcome() { add_action( 'wpmu_new_blog', 'dpa_handle_blogwelcome', 10, 0 ); }

//BP; these have to use counters, mostly because of XMLRPC.
//TODO: bp bug. Action missing.
function dpa_register_forumranked() { /*add_action( 'bp_forums_new_topic', 'dpa_handle_forumranked', 10, 0 );*/ }
//TODO: bp bug. Action missing.
function dpa_register_replyranked() { /*add_action( 'bp_forums_new_post', 'dpa_handle_replyranked', 10, 0 );*/ }

function dpa_register_wireranked() { add_action( 'bp_wire_post_posted', 'dpa_handle_wireranked', 10, 0 ); }

//BP
function dpa_register_friendask() { add_action( 'friends_friendship_requested', 'dpa_handle_friendask', 10, 0 ); }

//TODO: bp bug.  Action missing.
function dpa_register_groupcreated() { /*add_action( 'groups_created_group', 'dpa_handle_groupcreated', 10, 0 );*/ }

function dpa_register_groupinvitation() { add_action( 'groups_invite_user', 'dpa_handle_groupinvitation', 10, 0 ); }
function dpa_register_groupjoined() { add_action( 'groups_join_group', 'dpa_handle_groupjoined', 10, 0 ); }
function dpa_register_grouppromoted() { add_action( 'groups_promoted_member', 'dpa_handle_grouppromoted', 10, 0 ); }

function dpa_register_messageranked() { add_action( 'messages_message_sent', 'dpa_handle_messageranked', 10, 0 ); }

function dpa_register_profileupdate() { add_action( 'xprofile_updated_profile', 'dpa_handle_profileupdate', 10, 0 ); }
function dpa_register_profileactivated() { add_action( 'bp_core_account_activated', 'dpa_handle_profileactivated', 10, 0 ); }

//TODO: bp bug. Action missing.
function dpa_register_profileavatar() { /*add_action( 'bp_core_avatar_save', 'dpa_handle_profileavatar', 10, 0 );*/ }


function dpa_handle_blogcommentranked() {
	global $bp;

	if ( !function_exists( 'bp_blogs_install' ) || defined( 'DPA_CHECKING' ) )
		return;
		

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_1' ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$count = BP_Blogs_Comment::get_total_recorded_for_user( $user_id );
	if ( $count >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $count >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $count >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_blogranked() {
	global $bp;

	if ( !function_exists( 'bp_blogs_install' ) || defined( 'DPA_CHECKING' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_1' ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$count = BP_Blogs_Post::get_total_recorded_for_user( $user_id );
	if ( $count >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $count >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $count >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_forumranked() {
	global $bp;

	if ( !function_exists( 'bp_forums_is_installed_correctly' ) || !bp_forums_is_installed_correctly() || defined( 'DPA_CHECKING' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( $one->counter >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $one->counter >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $one->counter >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_replyranked() {
	global $bp;

	if ( !function_exists( 'bp_forums_is_installed_correctly' ) || !bp_forums_is_installed_correctly() || defined( 'DPA_CHECKING' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( $one->counter >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $one->counter >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $one->counter >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_messageranked() {
	global $bp, $wpdb;

	if ( !function_exists( 'messages_install' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_1' ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(sender_id) FROM {$bp->messages->table_name_messages} WHERE sender_id = %d", $user_id ) );
	if ( $count >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $count >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $count >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_wireranked() {
	global $bp;

	if ( defined( 'DPA_CHECKING' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( $one->counter >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $one->counter >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $one->counter >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_profileavatar() {
	global $bp;

	if ( defined( 'DPA_CHECKING' ) )
		return;

	$profileavatar = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'profileavatar' ) );
	if ( $profileavatar->is_unlocked() )
	  return;

  $profileavatar->unlock_achievement();
}

function dpa_handle_profileactivated() {
	global $bp;

	//TODO: Need to check if user has been activated/not spam (when called by DPA_CHECKING).
	$profileactivated = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'profileactivated' ) );
	if ( $profileactivated->is_unlocked() )
	  return;

  $profileactivated->unlock_achievement();
}

function dpa_handle_profileupdate() {
	global $bp;

	if ( defined( 'DPA_CHECKING' ) )
		return;

	$profileupdate = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'profileupdate' ) );
	if ( $profileupdate->is_unlocked() )
	  return;

  $profileupdate->unlock_achievement();
}

function dpa_handle_groupjoined() {
	global $bp;

	if ( !function_exists( 'groups_install' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	if ( 0 == BP_Groups_Member::total_group_count( $user_id ) )
		return;

	$groupjoined = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupjoined' ) );
	if ( $groupjoined->is_unlocked() )
	  return;

  $groupjoined->unlock_achievement();
}

function dpa_handle_groupinvitation() {
	global $bp;

	if ( !function_exists( 'groups_install' ) || defined( 'DPA_CHECKING' ) )
		return;

	$groupinvitation = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'groupinvitation' ) );
	if ( $groupinvitation->is_unlocked() )
	  return;

  $groupinvitation->unlock_achievement();
}

function dpa_handle_groupcreated() {
	global $bp, $wpdb;

	if ( !function_exists( 'groups_install' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	if ( 0 == $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name} WHERE creator_id = %d", $user_id ) ) )
		return;

	$groupcreated = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupcreated' ) );
	if ( $groupcreated->is_unlocked() )
	  return;

 	$groupcreated->unlock_achievement();
}

function dpa_handle_grouppromoted() {
	global $bp;

	if ( !function_exists( 'groups_install' ) )
		return;

	$user_id = $bp->loggedin_user->id;
	$groups = groups_get_user_is_mod_of( $user_id );
	if ( $groups['total'] == 0 )
		return;

	$grouppromoted = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'grouppromoted' ) );
	if ( $grouppromoted->is_unlocked() )
	  return;

  $grouppromoted->unlock_achievement();
}

function dpa_handle_friendask() {
	global $bp, $wpdb;

	if ( !function_exists( 'friends_install' ) )
		return;
		
	$user_id = $bp->loggedin_user->id;
	if ( 0 == $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->friends->table_name} WHERE initiator_user_id = %d", $user_id ) ) )
		return;

	$friendask = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'friendask' ) );
	if ( $friendask->is_unlocked() )
	  return;

  $friendask->unlock_achievement();
}

function dpa_handle_blogwelcome() {
	global $bp;

	if ( defined( 'DPA_CHECKING' ) )
		return;

	$blogwelcome = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'blogwelcome' ) );
	if ( $blogwelcome->is_unlocked() )
	  return;

  $blogwelcome->unlock_achievement();
}

function dpa_get_defaultachievements() {
 	return array(
		array( 'category' => "Blog",
					 'short_name' => 'blogwelcome',
					 'name' => __("Welcome to the blogosphere", 'dpa' ),
					 'description' => __("Created a blog; get posting!", 'dpa' ),
					 'points' => 15 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_1',
					 'name' => __("First post", 'dpa' ),
					 'description' => __("Published your first blog entry.", 'dpa' ),
					 'points' => 15 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_30',
					 'name' => __("Flexible Demeanour", 'dpa' ),
					 'description' => __("Published your thirtieth blog entry.", 'dpa' ),
					 'points' => 45 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogranked_100',
					 'name' => __("The Ends Of Invention", 'dpa' ),
					 'description' => __("One hundred blog posts!", 'dpa' ),
					 'points' => 75 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_1',
					 'name' => __("It's My Party And I'll Sing If I Want To", 'dpa' ),
					 'description' => __("Commented on a blog post.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_30',
					 'name' => __("But Who's Counting?", 'dpa' ),
					 'description' => __("You've left 30 comments on blogs.", 'dpa' ),
					 'points' => 30 ),
		array( 'category' => "Blog",
					 'short_name' => 'blogcommentranked_100',
					 'name' => __("You'll Thank Me Later", 'dpa' ),
					 'description' => __("You've left 100 comments on blogs.", 'dpa' ),
					 'points' => 50 ),
 		array( 'category' => "Friends",
					 'short_name' => 'friendask',
					 'name' => __("I Choo-Choo-Choose You!", 'dpa' ),
					 'description' => __("You've sent a friendship request to a member of the site.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'grouppromoted',
 					 'name' => __("You've got the Bridge, Number One", 'dpa' ),
					 'description' => __("Promoted someone to a moderator of a group.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupcreated',
					 'name' => __("Open social", 'dpa' ),
					 'description' => __("Created a group.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupinvitation',
					 'name' => __("All are welcome", 'dpa' ),
					 'description' => __("Sent an invitation for someone to join a group which you moderate.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Groups",
					 'short_name' => 'groupjoined',
					 'name' => __("Bonjour!", 'dpa' ),
					 'description' => __("Joined a group.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_1',
					 'name' => __("First time for everything", 'dpa' ),
					 'description' => __("Posted on a wire.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_30',
					 'name' => __("Second time for everything", 'dpa' ),
					 'description' => __("You've left 30 wire posts.", 'dpa' ),
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'wireranked_100',
					 'name' => __("Third time for everything", 'dpa' ),
					 'description' => __("You've left 100 wire posts.", 'dpa' ),
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_1',
					 'name' => __("First Class post", 'dpa' ),
					 'description' => __("Sent a message to someone.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_30',
					 'name' => __("Second Class post", 'dpa' ),
					 'description' => __("Sent 30 messages.", 'dpa' ),
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'messageranked_100',
					 'name' => __("Special Delivery", 'dpa' ),
					 'description' => __("Sent 100 messages.", 'dpa' ),
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_1',
					 'name' => __("Never Talk To Strangers", 'dpa' ),
					 'description' => __("Started a forum topic.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_30',
					 'name' => __("Allow me to elaborate", 'dpa' ),
					 'description' => __("Started 30 forum topics.", 'dpa' ),
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'forumranked_100',
					 'name' => __("Happy Idiot Talk", 'dpa' ),
					 'description' => __("Started 100 forum topics.", 'dpa' ),
					 'points' => 50 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_1',
					 'name' => __("Frank Exchange Of Views", 'dpa' ),
					 'description' => __("Left a reply on a forum topic.", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_30',
					 'name' => __("Quietly Confident", 'dpa' ),
					 'description' => __("Replied to 30 forum topics.", 'dpa' ),
					 'points' => 30 ),
		array( 'category' => "Messaging",
					 'short_name' => 'replyranked_100',
					 'name' => __("Revisionist", 'dpa' ),
					 'description' => __("Replied to 100 forum topics.", 'dpa' ),
					 'points' => 50 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileupdate',
					 'name' => __("Identity crisis", 'dpa' ),
					 'description' => __("Updated the information on your profile.", 'dpa' ),
					 'points' => 15 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileactivated',
					 'name' => __("Welcome", 'dpa' ),
					 'description' => __("You've activated your account on this website - welcome!", 'dpa' ),
					 'points' => 10 ),
		array( 'category' => "Profile",
					 'short_name' => 'profileavatar',
					 'name' => __("She's got that Mona Lisa smile", 'dpa' ),
					 'description' => __("Uploaded or updated the avatar picture to your account.", 'dpa' ),
					 'points' => 15 )
	);
}
?>